<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts' // echarts theme

export default {
  name: 'five',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '1500px'
    },
    height: {
      type: String,
      default: '750px'
    }
  },

  data() {
    return {
      chart: null,
      pieChartData: {
        dataValue: [
          {value: 440, name: '企业信息'},
          {value: 220, name: '股东信息'},
          {value: 550, name: '主要人员信息'},
          {value: 770, name: '变更信息'},
          {value: 660, name: '年报信息'},
          {value: 880, name: '异常经营信息'},
          {value: 990, name: '企业失信信息'},
        ]
      },
      chgTimer: null,
      chartTimer: null,
      optionType: 0
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
    this.changeOptions()
  },
  // created() {
  //   this.initChart()
  // },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setPieOptions(this.pieChartData)
    },

    changeOptions() {
      this.chgTimer = setInterval(() => {
        if (this.optionType === 0) {
          this.setMapOptions(this.pieChartData)
          this.optionType = 1
        } else {
          this.setPieOptions(this.pieChartData)
          this.optionType = 0
        }
      }, 15 * 1000)
    },

    getLevelOption() {
      return [
        {
          itemStyle: {
            borderWidth: 0,
            gapWidth: 5
          }
        },
        {
          itemStyle: {
            gapWidth: 1
          }
        },
        {
          colorSaturation: [0.35, 0.5],
          itemStyle: {
            gapWidth: 1,
            borderColorSaturation: 0.6
          }
        }
      ];
    },
    setPieOptions({dataValue} = {}) {
      this.chart.setOption({
        tooltip: {
          show: true,
          trigger: 'item',
          formatter: '{a} <br/><br/> {b} : {c}',
          extraCssText: 'width: 512px; height: 100px;font-size: 36px; font-family: Arial;font-weight: bold;',
          textStyle: {
            fontStyle: 'normal',
            fontWeight: 'normal',
            fontFamily: 'sans-serif',
            color: '#000',
            fontsize: 60
          }
        },
        series: [
          {
            name: '数据种类',
            type: 'pie',
            radius: ['45%', '70%'],
            center: ['50%', '60%'],
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 1
            },
            // label: {
            //   show: true,
            //   position: 'center'
            // },
            labelLine: {
              show: false
            },
            emphasis: {
              label: {
                show: true,
                color: '#fff',
                fontSize: '50'
              }
            },
            data: dataValue,
            avoidLabelOverlap: false,
            animationDurationUpdate: 1000,
            // animationDuration: 2000,
            universalTransition: true
          }
        ]
      })
    },
    setMapOptions({dataValue} = {}) {
      this.chart.setOption({
        tooltip: {
          show: true,
          trigger: 'item',
          formatter: '{a} <br/><br/> {b} : {c}',
          extraCssText: 'width: 512px; height: 100px;font-size: 36px; font-family: Arial;font-weight: bold;',
          textStyle: {
            fontStyle: 'normal',
            fontWeight: 'normal',
            fontFamily: 'sans-serif',
            color: '#000',
            fontsize: 60
          }
        },
        series: [
          {
            name: '各类数据数量',
            type: 'treemap',
            visibleMin: 300,
            width: '80%',
            height: '70%',
            label: {
              show: true,
              formatter: '{b}\n\n{c}',
              fontSize: '36'
            },
            itemStyle: {
              borderColor: 'rgba(255,255,255,0)'
            },
            levels: this.getLevelOption(),
            data: dataValue,
            animationDurationUpdate: 1000,
            // animationDuration: 2000,
            // avoidLabelOverlap: false
            universalTransition: true
          }
        ]
      })
    }
  }
}
</script>
